<template>
  <div style="height: 100%; width: 100%; position: relative">
      <div class="pagetop" ref="buttondiv">
          <el-input v-model="inputno" ref="txtinputno" @keyup.enter.native="txtinputnoenter" @focus="inputnogetfocus()" placeholder="输入就诊卡或病历号提取病人信息" style="margin-left:10px;width: 200px;"></el-input>
          <div class="search-title">快速查找：</div>
          <el-input style="width: 200px" v-model="searchtext" placeholder="病历号、姓名、身份证号"></el-input>
          <div class="operation-list">
              <el-button @click="QueryData()" icon="el-icon-search" v-has="'PINQUERY'" type="primary">查询</el-button>
              <el-button @click="OpenDialog(1)" icon="el-icon-plus" v-has="'PADD'">新增</el-button>
              <el-button @click="OpenDialog(2)" icon="el-icon-edit" v-has="'PMODIFY'">修改</el-button>
              <el-button @click="DelSpecialPat()" icon="el-icon-delete" v-has="'PDELETE'">删除</el-button>
          </div>
      </div>
      <div class="pagecenter">
          <rm-layout-left splitcolor="white" movingcolor="#00ffff" :splitwidth=5 fixsize="70%" :minfixsize=80>
              <template v-slot:fix>
                  <div style="height:100%">
                      <vxe-table :data="list" :loading="loading" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" :select-config="{ trigger: 'row' }" :sort-config="{ trigger: 'cell' }" @current-change="currentChangeEvent" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="xTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe>
                          <vxe-table-column type="index" title="NO." width="40" align="center"></vxe-table-column>
                          <vxe-table-column field="specialpatid" title="特殊患者ID" width="80" align="center"></vxe-table-column>
                          <vxe-table-column field="pat_no" title="病历号" width="100"></vxe-table-column>
                          <vxe-table-column field="pat_name" title="姓名" width="100"></vxe-table-column>
                          <vxe-table-column field="pat_sex" title="性别" width="50">
                              <template v-slot="{ row }">
                                  <span v-if="row.pat_sex == '1'">男</span>
                                  <span v-else-if="row.pat_sex == '2'">女</span>
                                  <span v-else>未知</span>
                              </template>
                          </vxe-table-column>
                          <vxe-table-column field="pat_agestr" title="年龄" width="60"></vxe-table-column>
                          <vxe-table-column field="pat_cardno" title="就诊卡号" width="120"></vxe-table-column>
                          <vxe-table-column field="pat_id" title="病人ID" width="100"></vxe-table-column>
                          <vxe-table-column field="inp_id" title="就诊号" width="120"></vxe-table-column>
                          <vxe-table-column field="pat_idcardno" title="身份证号/护照号" width="140"></vxe-table-column>
                          <vxe-table-column field="pat_birth" title="生日" width="100" :formatter="format"></vxe-table-column>
                          <vxe-table-column field="hospcode" title="送检单位/医院" width="160"></vxe-table-column>
                          <vxe-table-column field="lastregister_dt" title="最后登记日期" width="100" :formatter="format"></vxe-table-column>
                      </vxe-table>
                  </div>
              </template>
              <div style="height:100%">
                  <vxe-table :data="ItemData" :loading="itemloading" :keyboard-config="keyConfig" :keyboard-local-config="keyLocalConfig" :select-config="{ trigger: 'row' }" :sort-config="{ trigger: 'cell' }" @current-change="itemCurrentChangeEvent" border fit height="auto" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="xItemTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe>
                      <vxe-table-column type="index" title="NO." width="40" align="center"></vxe-table-column>
                      <vxe-table-column field="rpt_itemname" title="关注项目" width="120"></vxe-table-column>
                      <vxe-table-column field="register_dt" title="登记日期" width="100" :formatter="format"></vxe-table-column>
                      <vxe-table-column field="deadline_dt" title="截止日期" width="100" :formatter="format"></vxe-table-column>
                      <vxe-table-column field="concern_reason" title="关注理由" width="120"></vxe-table-column>
                  </vxe-table>
                  <template>
                      <div style="overflow: hidden;position: absolute;left:20px;bottom: 20px;z-index: 1;width: 200px;">
                          <el-button @click="btnAddItem" class="el-button--310">新增项目</el-button>
                          <el-button @click="btnUpdateItem" class="el-button--310">修改项目</el-button>
                          <el-button @click="btnDeleteItem" class="el-button--310">删除项目</el-button>
                      </div>
                  </template>
              </div>
          </rm-layout-left>
      </div>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" :title="dialogTitle" :visible.sync="dialogFormVisible" width="900px" height="500px" v-if="dialogFormVisible">
          <div>
              <el-form :inline="true" :model="PatInfo" @submit.native.prevent label-width="auto" ref="form">
                  <table height="100%" width="100%" style="width: 100%" class="tablelayout" ref="inputtable">
                      <tr>
                          <td align="right">病历号:</td>
                          <td>
                              <el-input v-model="PatInfo.pat_no" style="width: 100%;"></el-input>
                          </td>
                          <td align="right">姓名:</td>
                          <td>
                              <el-input v-model="PatInfo.pat_name" style="width: 100%;"></el-input>
                          </td>
                          <td align="right">性别:</td>
                          <td>
                              <el-select v-model="PatInfo.pat_sex" style="width: 100%" clearable>
                                  <el-option label="男" value="1"></el-option>
                                  <el-option label="女" value="2"></el-option>
                                  <el-option label="未知" value="3"></el-option>
                              </el-select>
                          </td>
                      </tr>
                      <tr>
                          <td align="right">年龄:</td>
                          <td>
                              <el-input v-model="PatInfo.pat_agestr" style="width: 100%;"></el-input>
                          </td>
                          <td align="right">就诊卡号:</td>
                          <td>
                              <el-input v-model="PatInfo.pat_cardno" style="width: 100%;"></el-input>
                          </td>
                          <td align="right">就诊号:</td>
                          <td>
                              <el-input v-model="PatInfo.inp_id" style="width: 100%;"></el-input>
                          </td>
                      </tr>
                      <tr>
                          <td align="right">身份证号/护照号:</td>
                          <td>
                              <el-input v-model="PatInfo.pat_idcardno" style="width: 100%;"></el-input>
                          </td>
                          <td align="right">生日:</td>
                          <td>
                              <el-date-picker type="date" placeholder="选择日期" v-model="PatInfo.pat_birth" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                          </td>
                          <td align="right">送检单位/医院:</td>
                          <td>
                              <el-input v-model="PatInfo.hospcode" style="width: 100%;"></el-input>
                          </td>
                      </tr>
                      <tr>
                          <td align="right">病人ID:</td>
                          <td>
                              <el-input v-model="PatInfo.pat_id" style="width: 100%;"></el-input>
                          </td>
                          <td align="right">最后登记日期:</td>
                          <td>
                              <span>{{PatInfo.lastregister_dt}}</span>
                          </td>
                      </tr>
                  </table>
              </el-form>
              <div class="dialog-footer" slot="footer" style="text-align: right">
                  <table style="width: 100%">
                      <tr>
                          <td width="660">
                              <span v-if="copyinfostr != ''" style="color:blue;margin-right:10px;">{{copyinfostr}}</span>
                          </td>
                          <td>
                              <el-button  type="primary" @click="CopyItems()">复制其他患者的关注项目</el-button>
                          </td>
                          <td>
                              <el-button @click="CommitTips()">确 定</el-button>
                          </td>
                          <td>
                              <el-button @click="CancelTips()">取 消</el-button>
                          </td>
                      </tr>
                  </table>
              </div>
          </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" :title="ItemTitle" :visible.sync="dialogItemVisible" width="800px" height="500px" v-if="dialogItemVisible">
          <div>
              <el-form :inline="true" :model="ItemInfo" @submit.native.prevent label-width="auto" ref="form">
                  <table height="100%" width="100%" style="width: 100%" class="tablelayout" ref="inputtable">
                      <tr>
                          <td align="right">关注项目</td>
                          <td>
                              <el-input v-if="itemactionflag == 2" v-model="ItemInfo.rpt_itemname" disabled></el-input>
                              <rm-ddl-rptitem v-else prgno="LIS163" :germmode="false" v-model="ItemInfo.rpt_itemcode" width="100%"></rm-ddl-rptitem>
                          </td>
                          <td align="right">截止日期</td>
                          <td>
                              <el-date-picker type="date" placeholder="选择日期" v-model="ItemInfo.deadline_dt" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                          </td>
                      </tr>
                      <tr>
                          <td align="right">关注理由</td>
                          <td colspan="5">
                              <el-input v-model="ItemInfo.concern_reason" type="textarea" :rows="3"></el-input>
                          </td>
                      </tr>
                  </table>
              </el-form>
              <div class="dialog-footer" slot="footer" style="text-align: right">
                  <table style="width: 100%">
                      <tr>
                          <td width="800"></td>
                          <td>
                              <el-button @click="ItemCommitTips()">确 定</el-button>
                          </td>
                          <td>
                              <el-button @click="ItemCancelTips()">取 消</el-button>
                          </td>
                      </tr>
                  </table>
              </div>
          </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" title="复制关注项目" :visible.sync="dialogCopyItemVisible" width="800px"  v-if="dialogCopyItemVisible">
          <div>
              <el-form :inline="true" :model="CopyItemInfo" @submit.native.prevent label-width="auto" ref="form">
                  <table height="100%" width="100%" style="width: 100%" class="tablelayout" ref="inputtable">
                      <tr>
                          <td align="right">被复制人特殊患者ID:</td>
                          <td>
                              <el-input v-model="CopyItemInfo.specialpatid" style="width: 100%;"></el-input>
                          </td>
                          <td align="right">截止日期</td>
                          <td>
                              <el-date-picker type="date" placeholder="选择日期" v-model="CopyItemInfo.deadline_dt" value-format="yyyy-MM-dd" style="width: 100%;"></el-date-picker>
                          </td>
                      </tr>
                  </table>
              </el-form>
              <div class="dialog-footer" slot="footer" style="text-align: right">
                  <table style="width: 100%">
                      <tr>
                          <td width="800"></td>
                          <td>
                              <el-button @click="CopyItemCommitTips()">确 定</el-button>
                          </td>
                          <td>
                              <el-button @click="CopyItemCancelTips()">取 消</el-button>
                          </td>
                      </tr>
                  </table>
              </div>
          </div>
      </el-dialog>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" title="提示" :visible.sync="dialogVisible" width="30%">
          <div class="el-message-box__status el-icon-warning"></div>
          <div class="el-message-box__message" style="padding-top: 24px">
              <p>
                  确定要删除特殊患者:<b style="color: red">{{ DelName }}</b>及其所有关注项目的信息吗?
              </p>
          </div>
          <span slot="footer" class="dialog-footer">
              <el-button @click="DelCommitTips()">确 定</el-button>
              <el-button @click="DelCancelTips()">取 消</el-button>
          </span>
      </el-dialog>
      <el-dialog v-el-drag-dialog :close-on-click-modal="false" title="提示" :visible.sync="dialogDelItemVisible" width="30%">
          <div class="el-message-box__status el-icon-warning"></div>
          <div class="el-message-box__message" style="padding-top: 24px">
              <p>
                  确定要删除关注项目:<b style="color: red">{{ DelItemName }}</b>吗?
              </p>
          </div>
          <span slot="footer" class="dialog-footer">
              <el-button @click="DelItemCommitTips()">确 定</el-button>
              <el-button @click="DelItemCancelTips()">取 消</el-button>
          </span>
      </el-dialog>
  </div>
</template>

<script>
import XEUtils from "xe-utils";
export default {
  name: "LIS163",
  data() {
      return {
          inputno: "",
          searchtext: "",
          keyConfig: {
              isDel: true,
              isTab: true,
              isEdit: true,
          },
          keyLocalConfig: {
              isArrow: true,
          },
          loading: false,
          PatData: [],
          ItemData: [],
          itemloading: false,
          dialogTitle: "",
          dialogFormVisible: false,
          PatInfo: {},
          dialogVisible: false,
          DelName: false,
          actionflag: 0,
          itemactionflag: 0,
          ItemInfo: {},
          ItemTitle: "",
          dialogItemVisible: false,
          dialogDelItemVisible: false,
          DelItemName: "",
          dialogCopyItemVisible: false,
          CopyItemInfo: {},
          copyinfostr: "",
      };
  },
  computed: {
      list() {
          let filterName = this.searchtext.trim();
          if (filterName) {
              let rest = this.PatData.filter((item) => {
                  return (item.pat_no ? item.pat_no.toUpperCase().indexOf(filterName.toUpperCase()) > -1 : false) || (item.pat_name ? item.pat_name.toUpperCase().indexOf(filterName.toUpperCase()) > -1 : false) ||  (item.pat_idcardno ? item.pat_idcardno.toUpperCase().indexOf(filterName.toUpperCase()) > -1 : false);
              });
              return rest;
          } else {
              return this.PatData;
          }
      },
  },
  created() {
      this.InitPage(); //初始化查询
  },
  methods: {
      QueryData() {
          this.loading = true;
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS163",
                  method: "post",
                  data: {
                      action: "querypatinfo",
                  },
              })
              .then((res) => {
                  this.loading = false;
                  this.PatData = res.patdata;
                  if(this.PatData.length > 0) {
                      this.$refs.xTable.setCurrentRow(this.PatData[0]);
                      this.DelName = this.PatData[0].pat_name;
                      this.QueryItemData(this.PatData[0]);
                  } else {
                      this.ItemData = [];
                  }
              });
      },
      QueryItemData(row) {
          if(row) {
              this.itemloading = true;
              this.$tool
                  .request({
                      url: "LIS/LIS1/LIS163",
                      method: "post",
                      data: {
                          action: "queryrptitem",
                          specialpatid: row.specialpatid,
                      },
                  })
                  .then((res) => {
                      this.itemloading = false;
                      this.ItemData = res.itemdata;
                      if(this.ItemData.length > 0) {
                          this.$refs.xItemTable.setCurrentRow(this.ItemData[0]);
                          this.DelItemName = this.ItemData[0].rpt_itemname;
                      }
                  });
          }
      },
      OpenDialog(type) {
          this.actionflag = type;
          if(type == 1){
              this.dialogTitle = "新增特殊患者信息"
              this.PatInfo = {
                  pat_no: "",
                  pat_id: "",
                  pat_name: "",
                  pat_sex: "1",
                  pat_agestr: "",
                  pat_cardno: "",
                  inp_id: "",
                  pat_idcardno: "",
                  pat_birth: "",
                  hospcode: "",
              };
          } else {
              var row = this.$refs.xTable.getCurrentRow();
              if(row){
                  this.PatInfo = XEUtils.clone(row, true);
              }
              else{
                  this.$message.warning("请选择病人信息！");
                  return;
              }
              this.dialogTitle = "修改特殊患者信息"
          }
          this.CopyItemInfo = {};
          this.copyinfostr = "";
          this.dialogFormVisible = true;
      },
      DelSpecialPat() {
          this.dialogVisible = true;
      },
      txtinputnoenter() {
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS163",
                  method: "post",
                  data: {
                      action: "getpatinfo",
                      inputno: this.inputno,
                  }
              })
              .then(res => {
                  if(res.flag == 1) {
                      this.PatInfo = res.patinfo;
                      this.actionflag = 1;
                      this.dialogTitle = "病人信息提取"
                      this.CopyItemInfo = {};
                      this.copyinfostr = "";
                      this.dialogFormVisible = true;
                  } else {
                      this.$message.error("提取病人信息失败:" + res.msg);
                  }
              });
      },
      inputnogetfocus() {
          this.$refs.txtinputno.select();
      },
      currentChangeEvent({ row }) {
          this.DelName = row.pat_name;
          this.QueryItemData(row);
      },
      itemCurrentChangeEvent({ row }) {
          this.DelItemName = row.rpt_itemname;
      },
      format({ cellValue, row, column }) {
          if (column.property == "pat_birth" || column.property == "lastregister_dt"|| column.property == "register_dt"|| column.property == "deadline_dt") {
                  return XEUtils.toDateString(row[column.property], "yyyy-MM-dd");
          }
      },
      btnAddItem() {
          var row = this.$refs.xTable.getCurrentRow();
          if(row){
              this.ItemInfo = {
                  specialpatid: row.specialpatid,
                  rpt_itemcode: "",
                  register_dt: XEUtils.toDateString(new Date(), "yyyy-MM-dd"),
                  deadline_dt: "",
                  concern_reason: "",
              };
              this.itemactionflag = 1;
              this.ItemTitle = "新增关注项目";
              this.dialogItemVisible = true;
          } else {
              this.$message.warning("请先选择要添加关注项目的病人信息！");
          }
      },
      btnUpdateItem() {
          var row = this.$refs.xTable.getCurrentRow();
          if(row){
              var itemrow = this.$refs.xItemTable.getCurrentRow();
              if(itemrow){
                  this.ItemInfo = itemrow;
                  this.ItemInfo.rpt_itemcode+="";
                  this.ItemTitle = "修改关注项目";
                  this.itemactionflag = 2;
                  this.dialogItemVisible = true;
              } else {
                  this.$message.warning("请先选择要修改的关注项目！");
              }
          } else {
              this.$message.warning("请先选择要修改关注项目的病人信息！");
          }
      },
      btnDeleteItem() {
          this.dialogDelItemVisible = true;
      },
      SaveData(type){
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS163",
                  method: "post",
                  data: {
                      action: "savedata",
                      patinfo: this.PatInfo,
                      type: type,
                      ...this.CopyItemInfo,
                  }
              })
              .then(res => {
                  if(res.flag == 1) {
                      if(res.msg != "") this.$message.warning("特殊患者信息保存成功！但" + res.msg);
                      else this.$message.success("保存成功！");
                      this.QueryData();
                  } else {
                      this.$message.error("保存失败！" + res.msg);
                  }
                  this.dialogFormVisible = false;
              });
      },
      SaveRptItemData(){
          var row = this.$refs.xTable.getCurrentRow();
          this.$tool
              .request({
                  url: "LIS/LIS1/LIS163",
                  method: "post",
                  data: {
                      action: "saverptitemdata",
                      ...this.ItemInfo,
                      type: this.itemactionflag,
                  }
              })
              .then(res => {
                  if(res.flag == 1) {
                      this.$message.success("保存成功！");
                      this.QueryItemData(row);
                      row.lastregister_dt = XEUtils.toDateString(new Date(), "yyyy-MM-dd");
                  } else {
                      this.$message.error("保存失败！" + res.msg);
                  }
                  this.dialogItemVisible = false;
              });
      },
      CopyItems() {
          this.CopyItemInfo = {
              specialpatid:"",
              deadline_dt: "",
          };
          this.dialogCopyItemVisible = true;
          this.copyinfostr = "";
      },
      CommitTips() {
          if (this.PatInfo.pat_no != "" && this.PatInfo.pat_name != "" ) {
              //检查下有没有病历号和姓名重复的，有的话不予保存
              if(this.PatData.length > 0 && this.actionflag == 1){
                  var havesamepat = this.PatData.filter(item=>  item.pat_no == this.PatInfo.pat_no && item.pat_name == this.PatInfo.pat_name );
                  if(havesamepat && havesamepat.length > 0) {
                      this.$message.warning("该患者信息已存在！");
                      return;
                  }
              }

              this.SaveData(this.actionflag);
          } else {
              this.$message.warning("病历号、姓名不能为空!");
          }
      },
      CancelTips() {
          this.dialogFormVisible = false;
      },
      DelCommitTips() {
          var row = this.$refs.xTable.getCurrentRow();
          if (row) {
              this.$tool
                  .request({
                      url: "LIS/LIS1/LIS163",
                      method: "post",
                      data: {
                          action: "deletepatinfo",
                          specialpatid: row.specialpatid,
                      }
                  })
                  .then(res => {
                      if(res.flag == 1) {
                          this.$message.success("删除成功！");
                          this.QueryData();
                      } else {
                          this.$message.error("删除失败！" + res.msg);
                      }
                      this.dialogVisible = false;
                  });
          }

      },
      DelCancelTips() {
          this.dialogVisible = false;
      },
      ItemCommitTips() {
          if (this.ItemInfo.rpt_itemcode != "" && this.PatInfo.deadline_dt != "" ) {
              this.SaveRptItemData();
          } else {
              this.$message.warning("关注项目、截止日期不能为空!");
          }
      },
      ItemCancelTips() {
          this.dialogItemVisible = false;
      },
      CopyItemCommitTips() {
          if(this.CopyItemInfo.specialpatid !="" && this.CopyItemInfo.deadline_dt !="")
          {
              this.dialogCopyItemVisible = false;
              this.copyinfostr = "被复制人特殊患者ID：" + this.CopyItemInfo.specialpatid + " 截止日期：" + this.CopyItemInfo.deadline_dt;
          } else {
              this.$message.warning("被复制人特殊患者ID、截止日期不能为空!");
          }
      },
      CopyItemCancelTips() {
          this.dialogCopyItemVisible = false;
          this.copyinfostr = "";
          this.CopyItemInfo = {};
      },
      DelItemCommitTips() {
          var row = this.$refs.xItemTable.getCurrentRow();
          if(row){
              this.$tool
                  .request({
                      url: "LIS/LIS1/LIS163",
                      method: "post",
                      data: {
                          action: "deleterptitem",
                          specialpatid: row.specialpatid,
                          rpt_itemcode: row.rpt_itemcode,
                      }
                  })
                  .then(res => {
                      if(res.flag == 1) {
                          this.$message.success("删除成功！");
                          this.QueryItemData(row);
                      } else {
                          this.$message.error("删除失败！" + res.msg);
                      }
                      this.dialogDelItemVisible = false;
                  });
          }
      },
      DelItemCancelTips() {
          this.dialogDelItemVisible = false;
      },
      InitPage() {
          this.QueryData();
      },
  },
}
</script>

<style scoped>
.pagetop {
  width: 100%;
  height: 35px;
  display: -webkit-box;
  -webkit-box-pack: left;
  -webkit-box-align: center;
  border: 1px solid #ccc;
  position: absolute;
  font-size: 12px;
  z-index: 99;
  overflow-x: auto;
  white-space: nowrap;
}
.operation-list {
  right: 5px;
  position: absolute;
  top: 5px;
  display: flex;
  align-items: center;
}
.search-title {
  margin: 0 5px 0 20px;
  font-size: 12px;
}
.pagecenter {
  position: absolute;
  top: 40px;
  width: 100%;
  left: 0;
  bottom: 0px;
  overflow: auto;
}
</style>
